package com.zdx.yswl.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.image.SmartImageView;
import com.umeng.analytics.MobclickAgent;
import com.zdx.yswl.R;
import com.zdx.yswl.R.color;
import com.zdx.yswl.http.KAsyncTask.TaskDelegate;
import com.zdx.yswl.tool.DisplayTool;
import com.zdx.yswl.tool.KStringTool;
import com.zdx.yswl.tool.KSystemTool;
import com.zdx.yswl.ui.adapter.BuildDetailViewPagerAdapter;
import com.zdx.yswl.ui.adapter.MainHousetypeAdapter;
import com.zdx.yswl.ui.base.BaseActivity;
import com.zdx.yswl.ui.bean.BuildDetailBean;
import com.zdx.yswl.ui.bean.HeadImgBean;
import com.zdx.yswl.ui.exception.NetworkException;
import com.zdx.yswl.ui.exception.NoDataException;
import com.zdx.yswl.ui.task.BuildDetailTask;
import com.zdx.yswl.view.horizontallistview.HorizontalListView;

public class DetailActivity extends BaseActivity implements OnClickListener, TaskDelegate {

	private String requestId;
	public static Intent actionStart(Context context, String requestId, String buildName) {
		Intent intent = new Intent(context, DetailActivity.class);
		intent.putExtra("projectId", requestId);
		intent.putExtra("buildName", buildName);
		return intent;
	}

	private ViewPager viewpager;
	private Button tel;
	private Button mapNav;
	private TextView mPrice;
	private TextView mDate;
	private TextView mAddrss;
	private TextView mDetailAddrss;
	private LinearLayout addTagView;

	private TextView mHouseCount;

	HorizontalListView mMain_houtype;
	MainHousetypeAdapter mAdapter;

	SmartImageView mBaiduMap;

	private  BuildDetailBean bean;

	@Override
	protected void initSettings()
	{

	}

	@Override
	protected void initUi()
	{

	}

	@Override
	protected void saveSettings()
	{

	}

	@Override
	protected void freeMe()
	{

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		Intent mIntent = getIntent();
		requestId = mIntent.getStringExtra("projectId");
		String buildName = mIntent.getStringExtra("buildName");
		if (KStringTool.isNotEmpty(buildName)) {
			setHeader(buildName);
		}
		initView();
	}

	private void initView() {

		// final ScrollView mScoll = (ScrollView)
		// findViewById(R.id.scollview_layout);
		viewpager = (ViewPager) findViewById(R.id.detail_head_pager);

		mPrice = (TextView) findViewById(R.id.detail_price_tv);
		mDate = (TextView) findViewById(R.id.detail_date_tv);
		mAddrss = (TextView) findViewById(R.id.detail_address_tv);
		mDetailAddrss = (TextView) findViewById(R.id.detail_wzzbaddress_tv);
		addTagView = (LinearLayout) findViewById(R.id.detail_tagview_layout); // 添加标签
		mapNav = (Button) findViewById(R.id.detail_map_nav_btn);// 打电话
		tel = (Button) findViewById(R.id.call_number);// 打电话
		tel.setOnClickListener(this);
		mHouseCount = (TextView) findViewById(R.id.mainhouse_count_tv);// 主力户型
																		// 数量

		Button mFullSince = (Button) findViewById(R.id.detail_btn1);// 3d全景
		Button mCamControll = (Button) findViewById(R.id.detail_btn2);// 摄像头控制
		mFullSince.setOnClickListener(this);
		mCamControll.setOnClickListener(this);

		RelativeLayout mDetailInfo = (RelativeLayout) findViewById(R.id.detail_detaileinfo_btn);// 详细信息
		RelativeLayout mNewsInfo = (RelativeLayout) findViewById(R.id.detail_newsinfo_btn);// 最新动态
		RelativeLayout mPhotoInfo = (RelativeLayout) findViewById(R.id.detail_photoinfo_btn);// 实景图片
		RelativeLayout mConsultant = (RelativeLayout) findViewById(R.id.detail_zygw_btn);// 置业顾问
		LinearLayout mAllType = (LinearLayout) findViewById(R.id.detail_housetype_info);// 主力户型查看全部
		RelativeLayout mDetailAddress = (RelativeLayout) findViewById(R.id.detail_detailaddress_btn);// 位置及周边
		mDetailInfo.setOnClickListener(this);
		mNewsInfo.setOnClickListener(this);
		mPhotoInfo.setOnClickListener(this);
		mConsultant.setOnClickListener(this);
		mAllType.setOnClickListener(this);
		mDetailAddress.setOnClickListener(this);

		/*
		 * MapView mMap = (com.baidu.mapapi.map.MapView)
		 * findViewById(R.id.bmapsView);// 百度地图 View view = mMap.getChildAt(0);
		 * view.setOnTouchListener(new OnTouchListener() {
		 * 
		 * @Override public boolean onTouch(View v, MotionEvent event) { if
		 * (event.getAction() == MotionEvent.ACTION_UP) {
		 * mScoll.requestDisallowInterceptTouchEvent(false); } else {
		 * mScoll.requestDisallowInterceptTouchEvent(true); } return false; }
		 * });
		 */

		mMain_houtype = (HorizontalListView) findViewById(R.id.main_house_type);
		mAdapter = new MainHousetypeAdapter(this);
		mMain_houtype.setAdapter(mAdapter);
		mBaiduMap = (SmartImageView) findViewById(R.id.bmapsView);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectId", requestId);
		BuildDetailTask task = new BuildDetailTask(-1, this);
		task.execute(map);
	}

	
	/**友盟session的统计*/
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	/**友盟session的统计*/
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.detail_btn1:
			if (KStringTool.isNotEmpty(requestId)) {
				startActivity(PanoramaActivity.actionStart(DetailActivity.this, requestId));
			}
			break;
		case R.id.detail_btn2:
//			startActivity(new Intent(this, CamActivity.class));
			startActivity(new Intent(this, DevMainActivity.class));
			break;
		case R.id.detail_detaileinfo_btn:
			if (KStringTool.isNotEmpty(requestId)) {
				startActivity(DetailedInforActivity.actionStart(DetailActivity.this,requestId));
			}
			break;
		case R.id.detail_newsinfo_btn:
			if (KStringTool.isNotEmpty(requestId)) {
				startActivity(NewsActivity.actionStart(DetailActivity.this, requestId));
			}
			break;
		case R.id.detail_photoinfo_btn:
			if (KStringTool.isNotEmpty(requestId)) {
				startActivity(AllPictureActivity.actionStart(DetailActivity.this, requestId));
			}
			
			break;
		case R.id.detail_zygw_btn:
			if (KStringTool.isNotEmpty(requestId)) {
				startActivity(ConsultantActivity.actionStart(DetailActivity.this, requestId));
			}
			break;
		case R.id.detail_housetype_info:
			if (bean != null) {
				startActivity(MainHouseTypeActivity.actionStart(DetailActivity.this, (ArrayList<HeadImgBean>) bean.houseType));
			}
			break;
		case R.id.detail_detailaddress_btn:
			if (bean != null) {
				startActivity(MapActivity.actionStart(DetailActivity.this,bean.latitude,bean.longitude ));
			}
			break;
		case R.id.call_number:
			String telNum = tel.getText().toString().trim();
			String[] strs = telNum.split(":");
			// if (KStringTool.isMobileNumb(strs[1])) { 手机号码 识别
			startActivity(KSystemTool.call_2(strs[1]));
			// }
			break;

		default:
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// backActivity();
			finish();
		}
		return true;
	}

	private void backActivity() {
		startActivity(new Intent(this, MainActivity.class));
		overridePendingTransition(R.anim.back_left_in, R.anim.back_right_out);
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		if (object instanceof NoDataException) {
			Toast.makeText(this, ((NoDataException) object).getMessage(), Toast.LENGTH_SHORT).show();;
		} else if (object instanceof NetworkException) {
//			ConfirmDialog.share(this, -1, new ConfirmDialogDelegate() {
//				@Override
//				public void onClick_ConfirmDialog(int __id, int __position) {
//					finish();
//				}
//			});
//			ConfirmDialog.showDialog("提示", ((AppException) object).getMessage(), "确定");
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();

		}
	}      

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		bean = (BuildDetailBean) object;
		BuildDetailViewPagerAdapter mPagerAdapter = new BuildDetailViewPagerAdapter(this, bean.cover);
		viewpager.setAdapter(mPagerAdapter);

		if ( bean.detailLabel != null) {
			int[] views = new int[bean.detailLabel.length];// 服务端传输的标签数组数量
			for (int i = 0; i < views.length; i++) {
				TextView tag = new TextView(this);
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
				params.setMargins(0, 0, DisplayTool.px2dip(this, 24), 0);
				tag.setLayoutParams(params);
				tag.setGravity(Gravity.CENTER);
				tag.setTextColor(getResources().getColor(color.main_while));
				tag.setText("  "+bean.detailLabel[i]+"  ");
				tag.setTextSize(DisplayTool.px2sp(this, 26));
				tag.setBackgroundResource(R.color.detail_tag_bg);
				addTagView.addView(tag);
			}
		}else {
			addTagView.setVisibility(View.GONE);
		}
		

		mAdapter.dataList = (ArrayList<HeadImgBean>) bean.houseType;
		mAdapter.notifyDataSetChanged();

		mPrice.setText("¥" + bean.price+"万");
		mDate.setText("时间：" + bean.openTime);
		mAddrss.setText("地址：" + bean.place);
		mDetailAddrss.setText("地址:" + bean.place);
		tel.setText("联系电话:" + bean.telPhone);
		mHouseCount.setText("( " + bean.houseType.size() + " )");
		mapNav.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MapActivity.actionMap(DetailActivity.this, bean.latitude,  bean.longitude, bean.place, bean.place);
			}

		});

		String width = String.valueOf(DisplayTool.px2dip(this, KSystemTool.getScreenWidth(this)));
		String height = String.valueOf(DisplayTool.px2dip(this, 430));
//		mBaiduMap.setImageUrl("http://api.map.baidu.com/staticimage?center=" + //
//				bean.longitude + "," + bean.latitude + "&width=" + width + "&height=" + height + "&zoom=" + "11"+"&markers="+bean.place +"&markerStyles=-1");
		mBaiduMap.setImageUrl("http://api.map.baidu.com/staticimage?" + "width=" + width + "&height=" + height + "&zoom=" + "11"+//
        "&markers="+bean.longitude+","+ bean.latitude +"&markerStyles=-1");
//		http://api.map.baidu.com/staticimage?zoom=15&markers=%D2%C3%BA%CD%D4%B0&markerStyles=-1,%E6%82%A8%E7%9A%84%E5%9B%BE%E7%89%87%E5%9C%B0%E5%9D%80,-1
	}

	@Override
	public void onNotify(int what, int arg1, int arg2, Object obj)
	{

	}

	@Override
	public void onHandler(int what, int arg1, int arg2, Object obj)
	{

	}
}
